using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Linq.Extensions;
using Abp.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using Sunlit.House.Floors.Dto;
using Abp.Extensions;
using Sunlit.House.Authorization;

namespace Sunlit.House.Floors
{
    public class FloorAppService: AsyncCrudAppService<Floor,FloorDto,long,GetAllFloorInput,
        CreateFloorInput,UpdateFloorInput>,IFloorAppService
    {
        private readonly IRepository<FloorImage, long> _imageRepository;
        public FloorAppService(IRepository<Floor,long> repository,
            IRepository<FloorImage, long> imageRepository) :base(repository)
        {
            CreatePermissionName = PermissionNames.Pages_Floor_Create;
            UpdatePermissionName = PermissionNames.Pages_Floor_Edit;
            DeletePermissionName = PermissionNames.Pages_Floor_Delete;

            _imageRepository = imageRepository;
        }
        protected override IQueryable<Floor> CreateFilteredQuery(GetAllFloorInput input)
        {
            return base.CreateFilteredQuery(input)
                .Include(f => f.Region)
                .Include(f => f.FloorImages)
                .Include(f=>f.Village)
                .WhereIf(!input.Title.IsNullOrWhiteSpace(), f => f.Title.Contains(input.Title) || f.Village.Name.Contains(input.Title))
                .WhereIf(input.RegionId.HasValue, f => f.RegionId == input.RegionId)
                .WhereIf(input.ShowOnHome.HasValue, f => f.ShowOnHome == input.ShowOnHome)
                .WhereIf(input.InSlider.HasValue, f => f.InSlider == input.InSlider)
                .WhereIf(input.CityId.HasValue, f => f.Region.CityId == input.CityId);
        }
        public override async Task<FloorDto> GetAsync(EntityDto<long> input)
        {
            var entity = await Repository.GetAll()
                .Include(e => e.Village)
                .Include(e => e.Region)
                .Include(e=>e.FloorImages)
                .FirstOrDefaultAsync(e => e.Id == input.Id);
            return ObjectMapper.Map<FloorDto>(entity);
        }
        public async Task<FloorDto> CreateEx(CreateFloorExInput input)
        {
            CheckCreatePermission();
            Floor floor = new();
            ObjectMapper.Map(input.Floor, floor);

            if (input.Village != null)
            {
                floor.Village = ObjectMapper.Map<Village>(input.Village);
            }
            
            if (input.ImageUrls != null)
            {
                floor.FloorImages = new List<FloorImage>();
                foreach (var item in input.ImageUrls)
                {
                    floor.FloorImages.Add(new FloorImage
                    {
                        ImageUrl = item.Trim(),
                        Title = floor.Title
                    });
                }
            }
            var inserted = await Repository.InsertAsync(floor);

            return ObjectMapper.Map<FloorDto>(inserted);
        }
        public async Task InSlider(EntityDto<long> input)
        {
            var entity = await Repository.GetAsync(input.Id);
            entity.InSlider = true;
        }
        public async Task OutSlider(EntityDto<long> input)
        {
            var entity = await Repository.GetAsync(input.Id);
            entity.InSlider = false;
        }
        public async Task ShowOnHome(EntityDto<long> input)
        {
            var entity = await Repository.GetAsync(input.Id);
            entity.ShowOnHome = true;
        }
        public async Task HideOnHome(EntityDto<long> input)
        {
            var entity = await Repository.GetAsync(input.Id);
            entity.ShowOnHome = false;
        }
    }
}
